package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.vo.PageData;
import cn.tedu.csmall.commons.web.JsonResult;
import cn.tedu.csmall.product.pojo.param.AttributeTemplateAddNewParam;
import cn.tedu.csmall.product.pojo.param.AttributeTemplateUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateStandardVO;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.service.IAttributeTemplateService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/attribute-templates")
@Api(tags = "06.属性模版管理模块")
@Slf4j
public class AttributeTemplateController {

    @Autowired
    private IAttributeTemplateService attributeTemplateService;

    @PostMapping("/add-new")
    @ApiOperation("添加属性模版")
    @ApiOperationSupport(order = 100)

    public JsonResult addNew(@Valid AttributeTemplateAddNewParam attributeTemplateAddNewParam) {
        log.debug("开始处理[添加属性模版]的请求，参数{}", attributeTemplateAddNewParam);
        attributeTemplateService.addNew(attributeTemplateAddNewParam);

        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("根据Id删除属性模版")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "属性模版ID", required = true, dataType = "long")

    public String delete(Long id) {

        return "";
    }
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("根据ID更新属性模板")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "属性模板ID", required = true, dataType = "Long")
    public JsonResult updateById(
            @Range(min = 1, message = "根据ID更新属性模板失败，请提交合法的ID值！")
            @PathVariable Long id,
            @Valid AttributeTemplateUpdateInfoParam attributeTemplateUpdateInfoParam){
        attributeTemplateService.updateInfoById(id, attributeTemplateUpdateInfoParam);

        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询属性模板详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "属性模板ID", required = true, dataType = "Long")
    })
    public JsonResult getStandardById(
            @Range(min = 1, message = "根据ID查询属性模板失败，请提交合法的ID值！")
            @PathVariable Long id){
        AttributeTemplateStandardVO standardById = attributeTemplateService.getStandardById(id);
        return JsonResult.ok(standardById);
    }


    @GetMapping("")
    @ApiOperation("查询属性模板列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码值", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult list(
            @Range(min = 1, message = "查询属性模板列表失败，请提供正确的页码值!") Integer pageNum,
            String queryType){
        if(pageNum == null || pageNum < 1){
            pageNum = 1;
        }
        PageData<AttributeTemplateListItemVO> pageData;
        if("all".equals(queryType)){
            pageData = attributeTemplateService.list(pageNum, Integer.MAX_VALUE);
        }else{
            pageData = attributeTemplateService.list(pageNum);
        }
        return JsonResult.ok(pageData);
    }



}
